package models

import (
	"time"

	"github.com/google/uuid"
)

// APIKey represents an API key for external access
type APIKey struct {
	ID          uuid.UUID  `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	Name        string     `json:"name" gorm:"not null"`
	Key         string     `json:"key" gorm:"uniqueIndex;not null"`
	Secret      string     `json:"-"` // Hidden in JSON
	UserID      uuid.UUID  `json:"user_id" gorm:"type:uuid;not null"`
	User        User       `json:"user" gorm:"foreignKey:UserID"`
	Permissions []string   `json:"permissions" gorm:"type:text[]"`
	RateLimit   int        `json:"rate_limit" gorm:"default:1000"` // requests per hour
	ExpiresAt   *time.Time `json:"expires_at"`
	LastUsedAt  *time.Time `json:"last_used_at"`
	IsActive    bool       `json:"is_active" gorm:"default:true"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`
}

// SystemMetrics represents system health metrics
type SystemMetrics struct {
	Timestamp      time.Time `json:"timestamp"`
	ActiveDevices  int64     `json:"active_devices"`
	TotalDevices   int64     `json:"total_devices"`
	MessagesPerSec float64   `json:"messages_per_sec"`
	ActiveUsers    int64     `json:"active_users"`
	SystemLoad     float64   `json:"system_load"`
	MemoryUsage    float64   `json:"memory_usage"`
	DiskUsage      float64   `json:"disk_usage"`
	NetworkTraffic float64   `json:"network_traffic"`
	ErrorRate      float64   `json:"error_rate"`
	ResponseTime   float64   `json:"response_time"`
}

// Notification represents a notification to be sent
type Notification struct {
	ID        uuid.UUID           `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	UserID    uuid.UUID           `json:"user_id" gorm:"type:uuid;not null"`
	User      User                `json:"user" gorm:"foreignKey:UserID"`
	Type      NotificationType    `json:"type" gorm:"not null"`
	Channel   NotificationChannel `json:"channel" gorm:"not null"`
	Title     string              `json:"title" gorm:"not null"`
	Message   string              `json:"message" gorm:"not null"`
	Data      NotificationData    `json:"data" gorm:"type:jsonb"`
	Status    NotificationStatus  `json:"status" gorm:"default:pending"`
	SentAt    *time.Time          `json:"sent_at"`
	ReadAt    *time.Time          `json:"read_at"`
	CreatedAt time.Time           `json:"created_at"`
	UpdatedAt time.Time           `json:"updated_at"`
}

type NotificationType string

const (
	NotificationTypeAlert   NotificationType = "alert"
	NotificationTypeInfo    NotificationType = "info"
	NotificationTypeWarning NotificationType = "warning"
	NotificationTypeError   NotificationType = "error"
)

type NotificationChannel string

const (
	NotificationChannelEmail     NotificationChannel = "email"
	NotificationChannelSMS       NotificationChannel = "sms"
	NotificationChannelPush      NotificationChannel = "push"
	NotificationChannelWebSocket NotificationChannel = "websocket"
	NotificationChannelSlack     NotificationChannel = "slack"
)

type NotificationStatus string

const (
	NotificationStatusPending   NotificationStatus = "pending"
	NotificationStatusSent      NotificationStatus = "sent"
	NotificationStatusDelivered NotificationStatus = "delivered"
	NotificationStatusFailed    NotificationStatus = "failed"
	NotificationStatusRead      NotificationStatus = "read"
)

type NotificationData struct {
	DeviceID  *uuid.UUID             `json:"device_id,omitempty"`
	AlertID   *uuid.UUID             `json:"alert_id,omitempty"`
	CommandID *uuid.UUID             `json:"command_id,omitempty"`
	Extra     map[string]interface{} `json:"extra,omitempty"`
}

// AuditLog represents audit trail for security and compliance
type AuditLog struct {
	ID         uuid.UUID              `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
	UserID     *uuid.UUID             `json:"user_id" gorm:"type:uuid"`
	User       *User                  `json:"user" gorm:"foreignKey:UserID"`
	Action     string                 `json:"action" gorm:"not null"`
	Resource   string                 `json:"resource" gorm:"not null"`
	ResourceID *uuid.UUID             `json:"resource_id" gorm:"type:uuid"`
	Details    map[string]interface{} `json:"details" gorm:"type:jsonb"`
	IPAddress  string                 `json:"ip_address"`
	UserAgent  string                 `json:"user_agent"`
	Success    bool                   `json:"success"`
	Error      string                 `json:"error"`
	Timestamp  time.Time              `json:"timestamp"`
}
